package edu.berkeley.cs160.prog1;


import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
//import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.EditText;
//import android.widget.PopupWindow;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class RestaurantActivity extends Activity implements OnClickListener, OnEditorActionListener {
	
	private ArrayList<String> actionList = new ArrayList<String>();
	private ArrayList<String> emotionList = new ArrayList<String>();
	
	//private Button button_emotions;
	//private Button button_actions;
	private Button button_help;
	private Button button_bye;

	
	private static final int emotion = 0;
	private static final int action = 1;
	private static final int help = 2;  //maybe use it to go back to cancel the dialog
	private static final int helpEmotion = 3;  //maybe use it to go back to cancel the dialog
	private static final int helpAction = 4;  //maybe use it to go back to cancel the dialog
	private static final int helpResponse = 5;  //maybe use it to go back to cancel the dialog
	//private static final int main_help_screen = 6; //go back to quick help main page
	//private static final int 
	
	public int touchCount = 0; //use to count the number of touches on msg box. 
							  //Start popups for 0, start typing msg when touchCount is 1
							  //set back to 0 when press "send" button in the textbox
	
	final CharSequence[] emotions = {"Fear", "Surprised", "Awe"};
	final CharSequence[] actions = {"Do Nothing", "Pointing", "Shake hands", "Cross arms"};
	final CharSequence[] helpList = {"How to change my facial expressions", "How to change my actions","How to send out a response","Quit Help"};
	
    @Override
    protected Dialog onCreateDialog(int id) {
    	AlertDialog popup;
        switch (id) {
        case emotion:
            AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
            builder1.setTitle("Pick an emotion");
            builder1.setItems(emotions, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                }
            });
            popup = builder1.create();
            
            break;
            
        case action:
            AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
            builder2.setTitle("Pick an action");
            builder2.setItems(actions, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                }
            });
            popup = builder2.create();
            
            break;
            
        case help:
            AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
            builder3.setTitle("Please select what you need help with");
            builder3.setItems(helpList, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) { //item is the index
                    //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                	switch (item){
                	case 0: //facial expression
                		showDialog(helpEmotion);
                		break;
                	case 1: //action
                		showDialog(helpAction);
                		break;
                	case 2: //response
                		showDialog(helpResponse);
                		break;
                	case 3: //quit
                		break;
                	default:
                		break;
                	}
                }
            });
            popup = builder3.create();
            
            break;
            
        case helpEmotion:
        	AlertDialog.Builder builder4 = new AlertDialog.Builder(this);
        	builder4.setMessage("The emotions are listed on the left side of game screen.\nTap the item from the list to select your emotion.")
            .setCancelable(false)
            .setPositiveButton("Go to Help main page", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     showDialog(help);
                }
            })
            .setNegativeButton("Quit Help", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
        });
            
        popup = builder4.create();
        break;
        
        case helpAction:
        	AlertDialog.Builder builder5 = new AlertDialog.Builder(this);
        	builder5.setMessage("The actions are listed on the left side of game screen.\nTap the item from the list to select your action.")
            .setCancelable(false)
            .setPositiveButton("Go to Help main page", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     showDialog(help);
                }
            })
            .setNegativeButton("Quit Help", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
        });
            
        popup = builder5.create();
        break;
        
        case helpResponse:
        	AlertDialog.Builder builder6 = new AlertDialog.Builder(this);
        	builder6.setMessage("To send a response,\ntype the words in the text box on\nbottom middle of the screen.\nTap the send button when done.")
            .setCancelable(false)
            .setPositiveButton("Go to Help main page", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     showDialog(help);
                }
            })
            .setNegativeButton("Quit Help", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
        });
            
        popup = builder6.create();
        break;
        
        
        default:
        	popup = null; //no match
        }
        return popup;
    } //end of onCreateDialog

	//TextView splitTotal;
	private View[] chatline = new View[17];
	private View[] responses = new View[5];
	int viewmode = 0;
	//View partner1 = findViewById(R.id.PartnerHeadImage);
    //View partner2 = findViewById(R.id.PartnerHeadImage2);
    //View partner3 = findViewById(R.id.PartnerHeadImage3);
    //View player1 = findViewById(R.id.PlayerPortrait);
    //View player2 = findViewById(R.id.PlayerPortrait2);
    //View player3 = findViewById(R.id.PlayerPortrait3);
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restaurant_view);
        chatline[0] = findViewById(R.id.ChatLine1);
        chatline[1] = findViewById(R.id.ChatLine2);
        chatline[2] = findViewById(R.id.ChatLine3);
        chatline[3] = findViewById(R.id.ChatLine4);
        chatline[4] = findViewById(R.id.ChatLine5);
        chatline[5] = findViewById(R.id.ChatLine6);
        chatline[6] = findViewById(R.id.ChatLine7);
        chatline[7] = findViewById(R.id.ChatLine8);
        chatline[8] = findViewById(R.id.ChatLine9);
        chatline[9] = findViewById(R.id.ChatLine10);
        chatline[10] = findViewById(R.id.ChatLine11);
        chatline[11] = findViewById(R.id.ChatLine12);
        chatline[12] = findViewById(R.id.ChatLine13);
        chatline[13] = findViewById(R.id.ChatLine14);
        chatline[14] = findViewById(R.id.ChatLine15);
        chatline[15] = findViewById(R.id.ChatLine16);
        chatline[16] = findViewById(R.id.ChatLine17);
        
        responses[0] = findViewById(R.id.choiceGroup1);
        responses[1] = findViewById(R.id.choiceGroup2);
        responses[2] = findViewById(R.id.choiceGroup3);
        responses[3] = findViewById(R.id.choiceGroup4);
        responses[4] = findViewById(R.id.choiceGroup5);
        //button_emotions = (Button)findViewById(R.id.EmotionsButton);
        //button_actions = (Button)findViewById(R.id.ActionButton);
        button_help = (Button)findViewById(R.id.HelpButton);
        button_bye = (Button) findViewById(R.id.ByeButton);
        
        Button emotionButton1 = (Button) findViewById(R.id.AngryButton);
        Button emotionButton2 = (Button) findViewById(R.id.SadButton);
        Button emotionButton3 = (Button) findViewById(R.id.SurprisedButton);
        Button emotionButton4 = (Button) findViewById(R.id.CalmButton);
        Button emotionButton5 = (Button) findViewById(R.id.HappyButton);
        Button emotionButton6 = (Button) findViewById(R.id.JoyousButton);
        
        //button_emotions.setOnClickListener((OnClickListener) this);
        //button_actions.setOnClickListener((OnClickListener) this);
        button_help.setOnClickListener((OnClickListener) this);
        button_bye.setOnClickListener((OnClickListener) this);
        emotionButton1.setOnClickListener((android.view.View.OnClickListener) this);
        emotionButton2.setOnClickListener((android.view.View.OnClickListener) this);
        emotionButton3.setOnClickListener((android.view.View.OnClickListener) this);
        emotionButton4.setOnClickListener((android.view.View.OnClickListener) this);
        emotionButton5.setOnClickListener((android.view.View.OnClickListener) this);
        emotionButton6.setOnClickListener((android.view.View.OnClickListener) this);
        
        for (int i=0;i<17;i++) {
        	chatline[i].setVisibility(View.GONE);
        }
        
        for (int j=0;j<5;j++) {
        	responses[j].setVisibility(View.GONE);
        }
        
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
			public void run() {
				chatline[viewmode].setVisibility(View.VISIBLE);
				responses[0].setVisibility(View.VISIBLE);
				viewmode++;
			}        	
        }, 2000);
        Button sendButton = (Button) findViewById(R.id.SendButton);
        sendButton.setOnClickListener(cReveal);
        
        
        //PopupWindow pw = new PopupWindow(findViewById(R.layout.aeselect_view),384,384,true);
        /*Intent myIntent = new Intent(this, Select.class);		//create a new activity!!@@@@@
        Bundle action = new Bundle();
        action.putStringArrayList("actions", actionList);
        action.putStringArrayList("emotions", emotionList);
        myIntent.putExtras(action);
        this.startActivity(myIntent);		//start a different activity!!!!!!@@@@@ */
    }
    

    OnClickListener cReveal = new OnClickListener() {
    	Handler handler = new Handler();
    	public void onClick (View v) {
    		if (viewmode==1) {
    			responses[0].setVisibility(View.GONE);
    			chatline[viewmode].setVisibility(View.VISIBLE);
    			viewmode++;
    			chatline[viewmode].setVisibility(View.VISIBLE);
    			viewmode++;
    	        handler.postDelayed(new Runnable() {
    				public void run() {
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    					responses[1].setVisibility(View.VISIBLE);
    				}        	
    	        }, 5000);
    	        //partner2.setVisibility(View.INVISIBLE);
    	        //partner1.setVisibility(View.VISIBLE);
    	        //findViewById(R.id.PlayerPortrait2).setVisibility(View.INVISIBLE);
    	        //findViewById(R.id.PlayerPortrait3).setVisibility(View.VISIBLE);
    		} else if (viewmode==4) {
    			responses[1].setVisibility(View.GONE);
    			chatline[viewmode-4].setVisibility(View.GONE);
    			chatline[viewmode++].setVisibility(View.VISIBLE);
    			handler.postDelayed(new Runnable() {
     				public void run() {
     					chatline[viewmode-4].setVisibility(View.GONE);
     					chatline[viewmode++].setVisibility(View.VISIBLE);
     				}        	
     	        }, 1000);
    			handler.postDelayed(new Runnable() {
    				public void run() {
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    				}        	
    	        }, 2000);
    	        findViewById(R.id.PartnerHeadImage2).setVisibility(View.INVISIBLE);
    	        handler.postDelayed(new Runnable() {
    				public void run() {
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    					responses[2].setVisibility(View.VISIBLE);
    				}        	
    	        }, 5000);

    	        findViewById(R.id.PartnerHeadImage3).setVisibility(View.VISIBLE);
    		} else if (viewmode==10) {
    			chatline[viewmode-4].setVisibility(View.GONE);
    			chatline[viewmode++].setVisibility(View.VISIBLE);
    			responses[2].setVisibility(View.GONE);
    			handler.postDelayed(new Runnable() {
    				public void run() {
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    					responses[3].setVisibility(View.VISIBLE);
    				}        	
    	        }, 1000);
    			
    	        //findViewById(R.id.PlayerPortrait3).setVisibility(View.INVISIBLE);
    	        //findViewById(R.id.PlayerPortrait).setVisibility(View.VISIBLE);
    		} else if (viewmode==12) {
    			responses[3].setVisibility(View.GONE);
    			chatline[viewmode-4].setVisibility(View.GONE);
    			chatline[viewmode++].setVisibility(View.VISIBLE);
    			handler.postDelayed(new Runnable() {
    				public void run() {
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    				}        	
    	        }, 1000);
       	        findViewById(R.id.PartnerHeadImage3).setVisibility(View.INVISIBLE);
    	        findViewById(R.id.PartnerHeadImage).setVisibility(View.VISIBLE);
    			handler.postDelayed(new Runnable() {
    				public void run() {
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    					chatline[viewmode-4].setVisibility(View.GONE);
    					chatline[viewmode++].setVisibility(View.VISIBLE);
    	    	        responses[4].setVisibility(View.VISIBLE);
    				}        	
    	        }, 5000);
    			
    	        //findViewById(R.id.PlayerPortrait).setVisibility(View.INVISIBLE);
    	        //findViewById(R.id.PlayerPortrait3).setVisibility(View.VISIBLE);
       	        findViewById(R.id.PartnerHeadImage).setVisibility(View.INVISIBLE);
    	        findViewById(R.id.PartnerHeadImage2).setVisibility(View.VISIBLE);
    		} else if (viewmode==16) {
    			findViewById(R.id.PartnerHeadImage2).setVisibility(View.INVISIBLE);
    	        findViewById(R.id.PartnerHeadImage3).setVisibility(View.VISIBLE);
    			chatline[viewmode-4].setVisibility(View.GONE);
    			chatline[viewmode++].setVisibility(View.VISIBLE);
    			responses[4].setVisibility(View.GONE);
    			
    		}
    		
    		else if (viewmode==17){
    			Intent myIntent = new Intent(v.getContext(), Feedback.class);		//create a new activity!!@@@@@
		        startActivity(myIntent);
    		}
    	}
    };
    
    public void onClick (View v) {
		switch(v.getId()) {
		case R.id.EmotionsButton:
			showDialog(emotion);
			break;
		case R.id.ActionButton:
			showDialog(action);
			break;
		case R.id.HelpButton:
			showDialog(help);
			break;
		case R.id.ByeButton:
			Intent myIntent = new Intent(v.getContext(), Feedback.class);		//create a new activity!!@@@@@
	        startActivity(myIntent);
		case R.id.AngryButton:
   	        findViewById(R.id.PlayerPortraitHappy).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitCalm).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitJoyous).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitSad).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitAngry).setVisibility(View.VISIBLE);
   	     	findViewById(R.id.PlayerPortraitSurprised).setVisibility(View.INVISIBLE);
			break;
		case R.id.SadButton:
   	        findViewById(R.id.PlayerPortraitHappy).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitCalm).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitJoyous).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitSad).setVisibility(View.VISIBLE);
   	        findViewById(R.id.PlayerPortraitAngry).setVisibility(View.INVISIBLE);
   	     	findViewById(R.id.PlayerPortraitSurprised).setVisibility(View.INVISIBLE);
			break;
		case R.id.SurprisedButton:
   	        findViewById(R.id.PlayerPortraitHappy).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitCalm).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitJoyous).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitSad).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitAngry).setVisibility(View.INVISIBLE);
   	     	findViewById(R.id.PlayerPortraitSurprised).setVisibility(View.VISIBLE);
			break;
		case R.id.CalmButton:
   	        findViewById(R.id.PlayerPortraitHappy).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitCalm).setVisibility(View.VISIBLE);
   	        findViewById(R.id.PlayerPortraitJoyous).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitSad).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitAngry).setVisibility(View.INVISIBLE);
   	     	findViewById(R.id.PlayerPortraitSurprised).setVisibility(View.INVISIBLE);
			break;
		case R.id.HappyButton:
   	        findViewById(R.id.PlayerPortraitHappy).setVisibility(View.VISIBLE);
   	        findViewById(R.id.PlayerPortraitCalm).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitJoyous).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitSad).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitAngry).setVisibility(View.INVISIBLE);
   	     	findViewById(R.id.PlayerPortraitSurprised).setVisibility(View.INVISIBLE);
			break;
		case R.id.JoyousButton:
   	        findViewById(R.id.PlayerPortraitHappy).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitCalm).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitJoyous).setVisibility(View.VISIBLE);
   	        findViewById(R.id.PlayerPortraitSad).setVisibility(View.INVISIBLE);
   	        findViewById(R.id.PlayerPortraitAngry).setVisibility(View.INVISIBLE);
   	     	findViewById(R.id.PlayerPortraitSurprised).setVisibility(View.INVISIBLE);
			break;
		default:
			break;
		}
    }


	public boolean onEditorAction(TextView splitEditor, int actionId, KeyEvent event) {
		return true;

	}



	public ArrayList<String> getActionList() {
		return actionList;
	}



	public void setActionList(ArrayList<String> actionList) {
		actionList.add("Do Nothing");
		actionList.add("Smile");
		actionList.add("Frown");
		actionList.add("Wave");
		this.actionList = actionList;
	}



	public ArrayList<String> getEmotionList() {
		return emotionList;
	}



	public void setEmotionList(ArrayList<String> emotionList) {
		emotionList.add("Happy");
		emotionList.add("Angry");
		emotionList.add("Sad");
		emotionList.add("Calm");
		this.emotionList = emotionList;
	}
    
}